﻿using System;
using System.Windows.Forms;
using System.Text;
using TCPIPClient;

namespace Communication
{
    public partial class Form1 : Form
    {
        commClient TCPClient = null;
        string _receivedData = "";
        
        public Form1()
        {
            InitializeComponent();
            TCPClient = new commClient();
            TCPClient.TransmissionReceived += this.getMessage;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TCPClient.Send(textBox2.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TCPClient.Connect("127.0.0.1", 9470, textBox3.Text);
        }

        private void getMessage(string message)
        {
            this._receivedData = message;
            PublishMessageInControl();
        }

        private void PublishMessageInControl()
        {
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(PublishMessageInControl));
            else
            {
                textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + this._receivedData;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.TCPClient.Disconnect();
        }

    }
}

